#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>

using namespace std;

void initLCA(const vector<int>& T, vector< vector<int> >& P, int n, int logN) {
    int i, j;

    for (i = 0; i < n; i++) { P[i][0] = T[i]; }

    for (j = 1; j < logN; j++) {
        for (i = 0; i < n; i++) {
            if (P[i][j - 1] != -1) { P[i][j] = P[P[i][j - 1]][j - 1]; }
        }
    }
}

int getLCA(const vector<int>& T,const vector< vector<int> >& P,const vector<int>& L, int a, int b) {
    if (L[a] < L[b]) { swap(a, b); }

    int logL = (int)floor(log((double)L[a]) / log(2.0));

    for (int i = logL; i >= 0; i--) {
        if (L[a] - (1 << i) >= L[b]) { a = P[a][i]; }
    }

    if (a == b) return a;

    for (int i = logL; i >= 0; i--) {
          if (P[a][i] != -1 && P[a][i] != P[b][i]) { a = P[a][i], b = P[b][i]; }
    }

    return T[a];
}


int main() {
    ifstream input("in.txt", ifstream::in);
    if (!input.is_open()) { cout<<"No input file!\n"; exit(1); }

    int n = 0;
    input>>n;
    n++; // для работы под acm.mipt.ru

    vector<int> T(n);
    vector<int> L(n, 0);
    int logN = (int)floor(log((double)n) / log(2.0));

    vector<int> tmp(logN, -1);
    vector< vector<int> > P(n, tmp);

    int a, b;
    for (int i = 0; i < n - 1; ++i) {
        input>>a>>b;
        T[a] = b;
        L[a] = L[b] + 1;
    }
    T[0] = -1;

    initLCA(T, P, n, logN);

    int m;
    input>>m;

    for (int i = 0; i < m; ++i) {
        input>>a>>b;
        cout<<getLCA(T, P, L, a, b)<<endl;
    }

    system("pause");
    return 0;
}


